package org.mspring.mlog.web.admin;

import org.mspring.mlog.common.exception.OperateException;
import org.mspring.mlog.web.BaseAdminController;
import org.mspring.mlog.entity.CategoryEntity;
import org.mspring.mlog.entity.UserEntity;
import org.mspring.mlog.service.CategoryEntityService;
import org.mspring.mlog.support.TabItem;
import org.mspring.nbee.common.PageResult;
import org.mspring.nbee.db.dao.sql.builder.part.condition.Operator;
import org.mspring.nbee.db.dao.sql.builder.part.condition.SqlCondition;
import org.mspring.nbee.db.dao.sql.pager.OrderType;
import org.mspring.nbee.db.dao.sql.pager.SqlPager;
import org.mspring.nbee.web.support.QueryParam;
import org.mspring.nbee.web.support.WebContext;
import org.mspring.nbee.web.view.json.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * @author Gao Youbo
 * @since 2014-12-05 11:51
 */
@Controller
@RequestMapping("/admin/category")
@TabItem(item = "100110")
public class CategoryController extends BaseAdminController {
    private CategoryEntityService categoryEntityService;

    @RequestMapping("/list")
    @TabItem(item = "100110005")
    public String list(WebContext context, Model model) {
        SqlPager pager = context.getPager();
        pager.addOrder("id", OrderType.DESC);
        List<SqlCondition> conditions = context.buildSqlConditions(
                model,
                new QueryParam(CategoryEntity.Fields.name, Operator.LIKE)
        );
        PageResult<CategoryEntity> result = categoryEntityService.list(conditions, pager);
        model.addAttribute("result", result);
        return "/admin/category/list";
    }

    @RequestMapping("/create")
    @TabItem(item = "100110010")
    public String create(WebContext context, Model model) {
        return "/admin/category/create";
    }

    @RequestMapping("/create/do")
    public String doCreate(WebContext context, Model model) {
        UserEntity currentUser = getCurrentUser();
        CategoryEntity category = context.fromRequest(CategoryEntity.class);
        category.setCreateTime(new Date());
        category.setCreateUser(currentUser.getId());
        category.setCreateUserName(currentUser.getNickname());
        category.setUpdateTime(new Date());
        category.setUpdateUser(currentUser.getId());
        category.setUpdateUserName(currentUser.getNickname());
        categoryEntityService.insert(category);
        return "redirect:/admin/category/list";
    }

    @RequestMapping("/edit")
    @TabItem(item = "100110015")
    public String edit(WebContext context, Model model) {
        Long id = context.getParamAsLong("id");
        if (id == null) {
            throw new OperateException("操作异常：请选择要编辑的分类");
        }
        CategoryEntity category = categoryEntityService.findById(id);
        model.addAttribute("category", category);
        return "/admin/category/edit";
    }

    @RequestMapping("/edit/do")
    public String doEdit(WebContext context, Model model) {
        UserEntity currentUser = getCurrentUser();
        long id = context.getRequiredLong("id");
        CategoryEntity category = categoryEntityService.findById(id);
        category = context.fromRequest(category);
        category.setUpdateTime(new Date());
        category.setUpdateUser(currentUser.getId());
        category.setUpdateUserName(currentUser.getNickname());
        categoryEntityService.update(category);
        return "redirect:/admin/category/list";
    }

    @RequestMapping("/delete/do")
    @ResponseBody
    public JsonResult delete(WebContext context, Model model) {
        long id = context.getRequiredLong("id");
        boolean flag = categoryEntityService.deleteById(id);
        return JsonResult.getJsonResult(flag);
    }

    @RequestMapping("/list/all")
    @ResponseBody
    public JsonResult listAll(WebContext context, Model model) {
        List<CategoryEntity> categoryList = categoryEntityService.listAll();
        return JsonResult.getJsonResult(categoryList);
    }

    @Autowired
    public void setCategoryEntityService(CategoryEntityService categoryEntityService) {
        this.categoryEntityService = categoryEntityService;
    }
}